package com.example.cloud.service.system.impl;

import com.example.cloud.dao.system.SystemConfigRepository;
import com.example.cloud.model.system.SystemConfig;
import com.example.cloud.model.system.User;
import com.example.cloud.service.system.SystemConfigService;
import com.example.cloud.utils.BusinessUtil;
import com.example.cloud.utils.RequestUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * Created by wangtao on 2018/2/3.
 */
@Service
public class SystemConfigServiceImpl implements SystemConfigService {

    private static final Logger logger = Logger.getLogger(SystemConfigServiceImpl.class);

    @Autowired
    private SystemConfigRepository systemConfigRepository;

    @Override
    public SystemConfig findOne() {
        User user = RequestUtil.getLoginUser();
        SystemConfig systemConfig = systemConfigRepository.findByAccountIdAndOrganId(user.getAccountId(), user.getOrganId());
        if(systemConfig ==  null){
            systemConfig = new SystemConfig();
        }
        return systemConfig;
    }

    @Override
    @Transactional
    public SystemConfig doSave(SystemConfig record) {
        if(record.getId() == null){
            User user = RequestUtil.getLoginUser();
            record.setAccountId(user.getAccountId());
            record.setUserId(user.getId());
            record.setOrganId(user.getOrganId());
            record.setOrganName(user.getOrganName());
        }
        BusinessUtil.appendRecord(record);
        return systemConfigRepository.save(record);
    }
}
